#!/bin/bash

# Slackware Build script for txt2tags

# Originally written 2009, eviljames <storgeek [at] gmail [dot] com>
# Now maintained by B. Watson <urchlay@slackware.uk>

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20220110 bkw:
# - take over maintenance.
# - update for v3.7.

# Note: the old version had a lot of things that are missing from
# the 3.x branch. See https://github.com/txt2tags/txt2tags/ for
# details. The 3.x branch has the sole advantage of being written in
# python 3. Since this is only included on SBo as a dependency for
# xdgmenumaker, I don't care... but maybe someone might want to do a
# txt2tags-legacy2 build if they need it.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=txt2tags
VERSION=${VERSION:-3.7}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.?z*
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

python3 setup.py install --root=$PKG

export PATH=$PATH:$PKG/usr/bin           
export PYTHONPATH=$PKG/$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"):$PYTHONPATH
cd docs
sh build-docs.sh
rm */*.t2t *.sh
cd -

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a COPYING *.md extras samples test docs/* $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
